package com.panda.mall.user.account.service.impl;

import cn.hutool.core.lang.generator.SnowflakeGenerator;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panda.mall.common.constants.BaseConstants;
import com.panda.mall.common.mallUser.dto.QueryUserDto;
import com.panda.mall.common.mallUser.entity.SystemUser;
import com.panda.mall.common.result.ServiceException;
import com.panda.mall.user.account.mapper.SystemAccountMapper;
import com.panda.mall.user.account.service.SystemAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author tucy123
* @description 针对表【system_user(用户表)】的数据库操作Service实现
* @createDate 2023-06-15 16:38:34
*/
@Service
public class SystemAccountServiceImpl extends ServiceImpl<SystemAccountMapper, SystemUser> implements SystemAccountService{

    @Autowired
    SystemAccountMapper systemAccountMapper;

    /**
     * 增改商城账户
     * @param systemUser
     * @return
     */
    @Override
    public Boolean saveOrUpdateUser(SystemUser systemUser) {
        //校验账号
        boolean matches = systemUser.getAccount().matches(BaseConstants.IS_digit_letter);
        if (!matches){
            throw new ServiceException("账号必须为数字或字母");
        }
        //是否新增用户
        boolean empty = ObjectUtil.isEmpty(systemUser.getId());

        this.saveOrUpdate(systemUser);

        //角色定位
        if (empty){
            //雪花算法生成id
            SnowflakeGenerator snowflakeGenerator = new SnowflakeGenerator();
            return systemAccountMapper.addAccountRole(snowflakeGenerator.next(),systemUser.getId(),systemUser.getRoleId());
        } else {
            //修改角色定位
            return systemAccountMapper.updateAccountRole(systemUser.getId(),systemUser.getRoleId());
        }
    }

    /**
     * 匹配查询商城账户
     * @return
     */
    @Override
    public Page<SystemUser> queryAllUser(QueryUserDto dto) {
        Page<SystemUser> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        //分页查询
        return systemAccountMapper.queryAllUser(page,dto);
    }

    /**
     * 查询商城账户详情
     * @param id
     * @return
     */
    @Override
    public SystemUser queryUserById(Long id) {
        return systemAccountMapper.queryUserById(id);
    }

    /**
     * 删除商城账户
     * @param ids
     * @return
     */
    @Override
    public Boolean deleteUser(List<Long> ids) {
        //查询账户信息
        List<SystemUser> systemUsers = systemAccountMapper.queryUsersByIds(ids);
        List<SystemUser> users = systemUsers.stream().filter(systemUser -> systemUser.getRoleId() == 1).collect(Collectors.toList());
        if (ObjectUtil.isNotEmpty(users)){
            throw new ServiceException("超管不能删除");
        }
        //删除账户信息
        systemAccountMapper.deleteBatchIds(ids);
        //删除角色定位
        return systemAccountMapper.deleteAccountRole(ids);
    }


}
